package com.bt.service.impl;

import com.bt.constant.MessageConstant;
import com.bt.constant.PasswordConstant;
import com.bt.constant.PasswordConstant;
import com.bt.constant.StatusConstant;
import com.bt.dto.YUserLoginDTO;
import com.bt.dto.YddUserDTO;
import com.bt.entity.YddUser;
import com.bt.mapper.YddUserMapper;
import com.bt.result.PageResult;
import com.bt.exception.AccountNotFoundException;
import com.bt.exception.PasswordErrorException;
import com.bt.exception.UserExistException;
import com.bt.mapper.YddUserMapper;
import com.bt.result.PageResult;
import com.bt.service.YddUserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import com.bt.utils.MD5Utils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.UUID;
import java.util.List;

/**
 * @Description TODO
 * @Author 锟
 * @Date 2023-04-29 17:50
 */
@Slf4j
@Service
public class YddUserServiceImpl implements YddUserService {

    @Autowired
    private YddUserMapper yddUserMapper;

    /**
     * 新增会员接口
     *
     * @param yddUserDTO
     */
    @Override
    public void add(YddUserDTO yddUserDTO) {
        //实例会员对象
        YddUser yddUser = new YddUser();

        //将前端的YddUserDTO的数据封装到实例对象
        BeanUtils.copyProperties(yddUserDTO,yddUser);

        //设置账号状态，默认为正常状态 1表示正常 0表示锁定
        yddUser.setStatus(StatusConstant.ENABLE);

        //设置账号状态，默认为男性
        yddUser.setSex(StatusConstant.ENABLE);

        //设置默认密码
        yddUser.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));

        yddUserMapper.add(yddUser);
    }

    /**
     * 会员分页查询接口
     *
     * @param page
     * @param limit
     * @param username
     * @param mobile
     * @param sex
     * @return
     */
   @Override
   public PageResult list(Integer page, Integer limit, String username, String mobile, String sex) {
       //参数设置
       PageHelper.startPage(page,limit);

       //执行查询返回数据列表
       List<YddUser> yddUsersList = yddUserMapper.page(username,mobile,sex);

       //数据列表转成Page<YddUser>
       Page<YddUser> pageInfo = (Page<YddUser>) yddUsersList;

       //封装PageResult返回
       return new PageResult(pageInfo.getTotal(),pageInfo.getPages(),pageInfo.getPageSize(),
               pageInfo.getPageNum(),pageInfo.getResult());

   }

    /**
     * 根据id查询会员回显接口
     *
     * @param id
     * @return
     */
    @Override
    public YddUser findById(Integer id) {
        return yddUserMapper.findById(id);
    }

    /**
     * 修改员工信息
     *
     * @param yddUserDTO
     */
    @Override
    public void update(YddUserDTO yddUserDTO) {

        //实例会员对象
       YddUser yddUser = new YddUser();
       BeanUtils.copyProperties(yddUserDTO,yddUser);
       //调用mapper修改
        yddUserMapper.update(yddUser);
    }

    /**
     * 删除员工信息
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        //删除会员数据
       yddUserMapper.deleteById(id);
    }
}
